// const cheerio = require('cheerio');
const axios = require('axios');
// const express = require('express');
// const app = express();
const mongoose = require('mongoose');
const Movies = require('./models/movies');

axios
    .get('http://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=24&page_id=3&ret_num=48&session=303dcde0d3ef65c847036d6e1407016f&three_category_id=1;must')
    .then(res => {
        const movies = [];
        res.data.data.list.forEach(item => {
            const movie = {};
            movie.title = item.name;
            movie.desc = item.description;
            movie.coverImg = item.imageUrl;
            movie.link = item.playUrl;
            movie.categories = item.categories.join(',');
            movies.push(movie);
        });
        mongoose
            .connect('mongodb://localhost:27017/movie', {
                useNewUrlParser: true,
                useUnifiedTopology: true,
            })
            .then(res => {
                Movies.insertMany(movies).then(() => {
                    console.log('数据存储成功');
                });
            });
    })